package com.atguigu.srb.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Description:
 * @Author: Gavin
 * @Date: 7/12/2023 6:43 PM
 */
@Configuration
public class GatewayConfig {
    //创建全局的跨域配置的filter,用来代替@CrossOrigin这个跨域的注解
    @Bean
    public CorsWebFilter corsWebFilter(){
        //基于路径配置跨域的参数
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("*");//允许跨域访问的服务器域名   * 通配所有
        config.addAllowedMethod("*");//允许所有方式的请求方式跨域访问
        config.addAllowedHeader("*");//允许携带所有头跨域访问
        config.setAllowCredentials(true);//允许携带cookie访问
        source.registerCorsConfiguration("/**",config);
        return new CorsWebFilter(source);
    }
}
